package cn.itcast.controller.cargo;

import cn.itcast.base.BaseController;
import cn.itcast.domain.cargo.*;
import cn.itcast.domain.vo.ContractProductVo;
import cn.itcast.service.ExportService;
import cn.itcast.service.PackingService;
import cn.itcast.service.ShippingService;
import cn.itcast.utils.DownloadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletContext;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/shipping")
public class ShippingController extends BaseController {

    @Reference
    private ShippingService shippingService;
    @Reference
    private PackingService packingService;
    @Reference
    private ExportService exportService;

    @RequestMapping("/list")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize) {

        ShippingExample shippingExample = new ShippingExample();
        shippingExample.setOrderByClause("create_time desc");
        PageInfo page = shippingService.findAll(shippingExample, pageNum, pageSize);
        request.setAttribute("pb", page);

        return "cargo/entrust/entrust-list";
    }

    @RequestMapping("/toAdd")
    public String toAdd(String packingId) {
        System.out.println(packingId);
        Packing packing = packingService.findById(packingId);
        if (packing.getState() == 0) {
            Shipping shipping = new Shipping();
            shipping.setShippingOrderId(packing.getPackingListId());
            shipping.setCompanyId(company_id);
            shipping.setCompanyName(company_name);
            String exportNos = packing.getExportNos();

            String ids = exportNos.split(" ")[0];
            Export export = exportService.findById(ids);
            shipping.setPortOfLoading(export.getShipmentPort());
            shipping.setPortOfDischarge(export.getDestinationPort());
            shipping.setLcNo(export.getLcno());
            shipping.setRemark(export.getRemark());

            request.setAttribute("shipping", shipping);
            request.setAttribute("packing", packing);
            request.setAttribute("export", export);
            return "cargo/entrust/entrust-add";
        } else {
            request.setAttribute("msg", "Can not be generated again!");
            return "error2";
        }
    }

    @RequestMapping("/delete")
    public String delete(String id) {
        Shipping shipping = shippingService.findById(id);
        Integer state = shipping.getState();
        if (state != 0) {
            request.setAttribute("msg", "Can not be deleted!");
            return "error2";
        }
        shippingService.delete(id);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping("/edit")
    public String edit(Shipping shipping) {
        shipping.setCreateTime(new Date());
        shipping.setState(0);
        shippingService.save(shipping);
        Packing packing = packingService.findById(shipping.getShippingOrderId());
        packing.setState(1);
        packingService.updateByDoMain(packing);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping("/submit")
    public String submit(String id) {
        Shipping shipping = shippingService.findById(id);
        Packing packing = packingService.findById(id);

        String exportNos = packing.getExportNos();
        String ids = exportNos.split(" ")[0];
        Export export = exportService.findById(ids);
        shipping.setPortOfLoading(export.getShipmentPort());
        shipping.setPortOfDischarge(export.getDestinationPort());
        shipping.setLcNo(export.getLcno());
        shipping.setRemark(export.getRemark());

        request.setAttribute("shipping", shipping);
        request.setAttribute("packing", packing);
        request.setAttribute("export", export);
        return "cargo/entrust/entrust-add";
    }

    @RequestMapping("/downloadExcel")
    public void downloadExcel(String id) throws IOException {
        Shipping shipping = shippingService.findById(id);
        Packing packing = packingService.findById(id);

        ServletContext servletContext = session.getServletContext();
        String path = servletContext.getRealPath("/make/xlsprint/海运委托书.xlsx");
        Workbook wb = new XSSFWorkbook(path);
        Sheet sheet = wb.getSheetAt(0);
        setExcelValue(sheet,2,4,shipping.getShipper());
        setExcelValue(sheet,6,4,shipping.getConsignee());
        setExcelValue(sheet,8,4,shipping.getNotifyParty());
        setExcelValue(sheet,10,4,shipping.getPortOfLoading());
        setExcelValue(sheet,12,4,shipping.getPortOfDischarge());
        setExcelValue(sheet,14,4,packing.getTotalVolume().toString());
        setExcelValue(sheet,16,4,shipping.getRemark());
        setExcelValue(sheet,18,4,shipping.getIsBatch());
        setExcelValue(sheet,10,10,shipping.getPortOfTrans());
        setExcelValue(sheet,12,10,packing.getMarks());
        setExcelValue(sheet,14,10,packing.getGrossWeights().toString());
        setExcelValue(sheet,16,10,shipping.getSpecialCondition());
        setExcelValue(sheet,18,10,shipping.getIsTrans());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        setExcelValue(sheet,27,9,date);

        //3 下载准备好的委托书
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        DownloadUtil downloadUtil = new DownloadUtil();
        wb.write(byteArrayOutputStream);
        downloadUtil.download(byteArrayOutputStream, response, "海运委托书.xlsx");


    }

    public void setExcelValue(Sheet sheet, int i, int j, String value) {

        Row row = sheet.getRow(i);
        Cell cell = row.getCell(j);
        if (i == 18){
            System.out.println(String.valueOf(value));
            System.out.println(String.valueOf(value).equals("1"));
            if (String.valueOf(value).equals("1")){
                cell.setCellValue("✔");
            }else{
                cell.setCellValue("×");
            }
            return;
        }
        cell.setCellValue(value);
    }

}
